#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "../include/game.h"
#include "../include/misc.h"
#include "../include/liste.h"

struct s_list {
	t_bomb bomb;
	int number_bomb;
	struct s_list *next;
	struct s_list *prec;
};

int list_is_empty(t_list l) {
	if (l->bomb == NULL && l->next == NULL )
		return EXIT_SUCCESS;
	else
		return EXIT_FAILURE;
}

int list_is_empty_2(t_list l) {
	if (l->number_bomb == 0)
		return EXIT_SUCCESS;
	else
		return EXIT_FAILURE;
}

t_list list_new() {
	t_list l = malloc(sizeof(struct s_list));
	assert(l);
	l->bomb = NULL;
	l->next = NULL;
	l->prec = NULL;
	l->number_bomb = 0;
	return l;
}



t_list list_add(t_list l, t_bomb nbomb) {
	t_list l1 = list_new();
	l1->bomb = nbomb;
/*if (list_print_bomb(l) == NULL) {
		return l1;
	}*/
	l1->next = l;
	l->prec=l1;
	l1->number_bomb=l->number_bomb + 1;
	return l1;
}

void list_print(t_list l) {
	if (!list_is_empty(l)) {
		list_print(l->next);
	}
}

void list_number(t_list l) {
	assert(l);
	int x = 0;
	t_list l1 = l;
	while (!list_is_empty(l1)) {
		x = x + 1;
		l1 = l1->next;

	}
	printf("Il y a %d elements dans la liste.", x);
}


void list_number_2(t_list l) {
	assert(l);
	printf("Il y a %d elements dans la liste.",l->number_bomb);
}



t_bomb list_print_bomb(t_list l) {
	assert(l);
	return l->bomb;
}

t_list list_print_next(t_list l) {
	return l->next;
}

t_list list_print_prec(t_list l) {
	return l->prec;
}

int get_bomb_number(t_list l) {
	return l->number_bomb;
}

/*void update_list_status (t_list l) {
	assert(l);
	while (l->bomb != NULL && l->next != NULL) {
		update_bomb_status (list_print_bomb(l));
		l=list_print_next(l);
	}
	while (l->prec != NULL) {
		l=l->prec;
	}
}

void update_list_images (t_list l,SDL_Surface *screen) {
	assert(l);
	assert(screen);
	update_list_status (l);
	while (l->bomb != NULL && l->next != NULL) {
		display_bomb(list_print_bomb(l),screen);
		l=list_print_next(l);
	}
	while (l->prec != NULL) {
			l=l->prec;
		}
}*/

t_list remove_from_list(t_list l) {

	if (l->prec == NULL && l->next != NULL){ /*Dernier élèment restant de la liste hors case initiale*/
		//l=list_add(l,NULL);
		free(l->bomb);
		l=l->next;
		l->prec=NULL;
		/*l->next->prec=l->prec;
		l=l->prec;*/
	}
	/*else if (l->prec == NULL && l->next != NULL){ élément de la liste
		l=l->prec;
		l->next=NULL;
	}*/
	else { /*Elément dans la liste*/
		free(l->bomb);
		l->prec->next=l->next;
		l->next->prec=l->prec;
		l=l->prec;
	}

	return l;
}


